<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { findListApi } from "@/api/system/dict"
import { type FormInstance } from "element-plus"
import { Search, Refresh, Download, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import type { VxeTablePropTypes, VxeTableEvents } from "vxe-table"
import DictDet from "./components/DictDet.vue"

defineOptions({
  name: "dict"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const pageData = ref({
  dialogErrorMessageVisible: false,
  dialogDetPage: false,
  currentShowErrorMessage: "",
  mainRowData: {} as any
})

//#region 查
const tableData = ref<any>([])
const tableDetData = ref<any>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  onlineCode: "",
  invoiceSn: ""
})
const getTableData = () => {
  loading.value = true
  findListApi({
    startPage: paginationData.startPage,
    pageSize: paginationData.pageSize,
    ...searchData
  })
    .then((res) => {
      paginationData.total = res.count
      tableData.value = res.data
      console.log(tableData.value)
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.startPage === 1 ? getTableData() : (paginationData.startPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

const rowDbclick = (row: any) => {
  console.log("row: ", row)
  pageData.value.mainRowData = row
  pageData.value.dialogDetPage = true

  // loading.value = true
}

//#endregion

interface RowVO {
  id: number
  name: string
  role: string
  sex: string
  age: number
  address: string
}

const rowConfig = reactive<VxeTablePropTypes.RowConfig<RowVO>>({
  useKey: true,
  drag: true
})

/** 监听分页参数的变化 */
watch([() => paginationData.startPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="下载">
            <el-button type="primary" :icon="Download" circle />
          </el-tooltip>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" @row-dblclick="rowDbclick">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="dictName" label="字典名称" align="center" />
          <el-table-column prop="dataDictCode" label="字典代码" align="center" />
          <el-table-column prop="createTime" label="创建时间" align="center" />
          <el-table-column prop="createUserName" label="创建人名称" align="center" />
          <el-table-column prop="modifiedTime" label="修改时间" align="center" />
          <el-table-column prop="modifiedUserName" label="修改人名称" align="center" />
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.startPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <el-dialog v-model="pageData.dialogErrorMessageVisible" title="异常信息" width="70%">
      <div class="error-log">{{ pageData.currentShowErrorMessage }}</div>
    </el-dialog>

    <el-dialog v-model="pageData.dialogDetPage" title="字典明细" width="70%">
      <DictDet :dictRowData="pageData.mainRowData" :key="pageData.mainRowData.dataDictCode" />
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;

  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
